import { ImuneTreeList, ImuneTreeArr, IappObj } from '@/types/common';

// 菜单数据
const muneTreeList: ImuneTreeList[] = [
  {
    appTitle: '权限管理',
    appCode: 'wanfeng',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng/appList', id: '2' },
      { title: '公共页面', path: '/wanfeng/publicApp', id: '3' },
    ]
  },
  {
    appTitle: '微应用-1-umi',
    appCode: 'wanfeng-app1',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app1/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app1/appList', id: '2' },
      { title: '公共页面', path: '/wanfeng-app1/publicApp', id: '3' },
      { title: '公共页面2', path: '/wanfeng-app1/publicApp2', id: '4' },
      { title: '微应用-页面一', path: '/wanfeng-app1/index1', id: '5' },
      { title: '微应用-页面二', path: '/wanfeng-app1/index2', id: '6' }
    ]
  },
  {
    appTitle: '微应用-2-umi',
    appCode: 'wanfeng-app2',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app2/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app2/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app2/index1', id: '3' },
      { title: '微应用-页面二', path: '/wanfeng-app2/index2', id: '4' }
    ]
  },
  {
    appTitle: '微应用-3-vue2.0',
    appCode: 'wanfeng-app3',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app3/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app3/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app3/index1', id: '3' },
    ]
  },
  {
    appTitle: '微应用-4-vue3.0-webpack',
    appCode: 'wanfeng-app4',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app4/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app4/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app4/index1', id: '3' },
    ]
  },
  {
    appTitle: '微应用-5-react-18',
    appCode: 'wanfeng-app5',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app5/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app5/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app5/index1', id: '3' },
    ]
  },
  {
    appTitle: '微应用-6-vue3.0-vite',
    appCode: 'wanfeng-app6',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app6/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app6/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app6/index1', id: '3' },
    ]
  },
  {
    appTitle: '微应用-7-react-17',
    appCode: 'wanfeng-app7',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app7/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app7/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app7/index1', id: '3' },
    ]
  },
  {
    appTitle: '微应用-8-react-16',
    appCode: 'wanfeng-app8',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app8/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app8/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app8/index1', id: '3' },
    ]
  },
  {
    appTitle: '微应用-9-传统非SPA应用',
    appCode: 'wanfeng-app9',
    muneTreeArr: [
      { title: '用户管理', path: '/wanfeng-app9/userAdmin', id: '1' },
      { title: '应用管理', path: '/wanfeng-app9/appList', id: '2' },
      { title: '微应用-页面一', path: '/wanfeng-app9/index1', id: '3' },
    ]
  },
];

// 应用数据
const appList: IappObj[] = [
  { title: '权限管理', url: 'http://192.168.77.10:8001/', path: '/wanfeng/userAdmin', appName: 'wanfeng', type:'' },
  { title: '微应用-1-umi', url: 'http://localhost:8001', path: '/wanfeng-app1/userAdmin', appName: 'wanfeng-app1',type:'umi' },
  { title: '微应用-2-umi', url: 'http://localhost:8002', path: '/wanfeng-app2/userAdmin', appName: 'wanfeng-app2',type:'umi' },
  { title: '微应用-3-vue2.0', url: 'http://localhost:8082/', path: '/wanfeng-app3/userAdmin', appName: 'wanfeng-app3' ,type:'vue'},
  { title: '微应用-4-vue3.0-webpack', url: 'http://localhost:10001/', path: '/wanfeng-app4/userAdmin', appName: 'wanfeng-app4',type:'vue' },
  { title: '微应用-5-react-18', url: 'http://localhost:3000', path: '/wanfeng-app5/userAdmin', appName: 'wanfeng-app5' ,type:'react'},
  { title: '微应用-6-vue3.0-vite', url: 'http://localhost:3001/', path: '/wanfeng-app6/userAdmin', appName: 'wanfeng-app6',type:'vue-vite' },
  { title: '微应用-7-react-17', url: 'http://localhost:3002/', path: '/wanfeng-app7/userAdmin', appName: 'wanfeng-app7',type:'react' },
  { title: '微应用-8-react-16', url: 'http://localhost:3003/', path: '/wanfeng-app8/userAdmin', appName: 'wanfeng-app8',type:'react' },
  { title: '微应用-9-传统非SPA应用', url: ' http://172.16.66.196:8080/', path: '/wanfeng-app9/userAdmin', appName: 'wanfeng-app9',type:'' }
];

// 查询当前应用所拥有的菜单数据
const getMuneTree = (req: any, res: any) => {
  // 获取传递过来的参数值
  const { url } = req.query;

  const muneTreeArr: ImuneTreeList[] = [];

  muneTreeList.forEach((item: ImuneTreeList) => {
    // 根据url得到当前应用名称，进行数据筛选
    if (item.appCode === url) {
      muneTreeArr.push(item);
    }
  });

  res.send({
    code: 0,
    data: muneTreeArr,
  });
};

// 筛选出除基座本身菜单并且是权限菜单的数据
const getAppMenuTree = (req: any, res: any) => {
  const muneTreeArr: ImuneTreeArr[] = [];

  muneTreeList.forEach((item: ImuneTreeList) => {
    item.muneTreeArr.forEach((item2: ImuneTreeArr) => {
      if (item2.path.split('/')[1] !== 'wanfeng' && item2.title === '用户管理' || item2.path.split('/')[1] !== 'wanfeng' && item2.title === '应用管理' || item2.path.split('/')[1] !== 'wanfeng' && item2.title === '公共页面' || item2.path.split('/')[1] !== 'wanfeng' && item2.title === '公共页面2') {
        muneTreeArr.push(item2);
      }
    })
  });

  res.send({
    code: 0,
    data: muneTreeArr,
  });
}

// 查询应用数据
const getAppSearch = (req: any, res: any) => {
  let appListArr: IappObj[] = [];
  const { appName } = req.query;


  if (appName) {
    appList.forEach((item: IappObj) => {
      if (item.appName === appName) {
        appListArr.push(item);
      }
    })
  }
  else {
    appListArr = appList;
  }

  res.send({
    code: 0,
    data: appListArr,
  });
}


export default {
  'Get /api/getMuneTree': getMuneTree,
  'Get /api/getAppMenuTree': getAppMenuTree,
  'Get /api/getAppSearch': getAppSearch,
};
